﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Encryption.Licensing;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;
using Rio.Framework.Pages;
using Rio.Framework.Pages.AppCode.Enums;
using Rio.Framework.Pages.AppCode.Helpers;
using Rio.Framework.Pages.ChildWindows;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Controls.Historic;
using Rio.Framework.Pages.Enums;
using Rio.Framework.Pages.Manager;
using Rio.Framework.Pages.MenuOffice;
using Rio.Framework.Pages.AppCode.ApplicationNavigation;

namespace Rio.Application.RioWeb.Home.Views
{
    public partial class HomeView
    {
        #region Properties

        public SearchHistoric SearchHistoricPanel { get; set; }
        public ImportHistoric ImportHistoricPanel { get; set; }
        public TimeoutAction TimeoutAction { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        ///   Initializes a new instance of the <see cref = "OfficePage" /> class.
        /// </summary>
        public HomeView()
        {
            InitializeComponent();

            TimeoutAction = new TimeoutAction(500);

            ChangePasswordHyperLinkButton.Visibility = LoginManager.Instance.AuthenticationMethod
                                                           == LoginDomainServiceEnums.AuthenticationMethod.SSO
                                                               ? Visibility.Collapsed
                                                               : Visibility.Visible;

            if (LoginManager.Instance.CurrentUser != null)
                Initialization();
            else
            {
                //if (LoginManager.Instance.AuthenticationMethod == LoginDomainServiceEnums.AuthenticationMethod.SSO)
                //{
                    LoginManager.Instance.AuthenticatedUser += (sender, args) => Initialization();
                //}
            }
        }

        #endregion

        /// <summary>
        ///   Inits the labels.
        /// </summary>
        private void InitLabels()
        {
            ManagerButton.Title = Resx.GetLabel(ResxMenu.MANAGER);
            FlowButton.Title = Resx.GetLabel(ResxMenu.FLOW);
            ImportButton.Title = Resx.GetLabel(ResxMenu.INDEX);
            SearchButton.Title = Resx.GetLabel(ResxMenu.SEARCH);
            ChangePasswordHyperLinkButton.Content = Resx.GetLabel(ResxOfficePage.ChangePassword_Title);
            busyIndicator.BusyContent = Resx.GetLabel(ResxCommon.LOADING_PAGE);
        }

        /// <summary>
        ///   Refreshes the specified user.
        /// </summary>
        /// <param name = "P_oUser">The p_o user.</param>
        public void Initialization()
        {
            headMenu.LoadUser(ApplicationName.RioOffice);

            labelName.Text = GlobalWebContext.CurrentUser.FullName;

            // Check right on each group
            foreach (Group grp in GlobalWebContext.CurrentUser.GroupList)
            {
                // Administrator : Active all right
                if (grp.IsAdministrator)
                {
                    ActiveButtonMenuVisibility(true, true, true);
                    FlowButton.Visibility = Visibility.Visible;
                    FlowButton.Visibility = GlobalWebContext.LicenseTerms.LicenseType == LicenseType.Enterprise
                                                ? Visibility.Visible
                                                : Visibility.Collapsed;
                    break;
                }

                // Active scan/import right
                if (grp.CanScann)
                {
                    ActiveButtonMenuVisibility(true, false, false);
                }

                // Active search right
                if ((grp.CanConsult) || (grp.CanReorganize) || (grp.CanDownload) || (grp.CanPrint)
                    || (grp.CanSendEmail))
                {
                    ActiveButtonMenuVisibility(false, true, false);
                }

                //Active flow
                FlowButton.Visibility = grp.CanFlow
                                        && GlobalWebContext.LicenseTerms.LicenseType == LicenseType.Enterprise
                                            ? Visibility.Visible
                                            : Visibility.Collapsed;
            }

            busyIndicator.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        ///   Active Visibility of button
        /// </summary>
        /// <param name = "P_bScanActivation">Scan and import Activation</param>
        /// <param name = "P_bSearchActivation">Search Activation</param>
        /// <param name = "P_bRioManager">Manager Activation</param>
        private void ActiveButtonMenuVisibility(bool P_bScanActivation, bool P_bSearchActivation, bool P_bRioManager)
        {
            //Desactivate Rio Manager Menu swith user group
            if (P_bRioManager)
            {
                ManagerButton.Visibility = Visibility.Visible;
            }

            //Desactivate scan button switch user group
            if (P_bScanActivation)
            {
                ImportButton.Visibility = Visibility.Visible;
            }

            //Desactivate scan button switch user group
            if (P_bSearchActivation)
            {
                SearchButton.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        ///   Handles the MouseEnter event of the ApplicationButton control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.Windows.Input.MouseEventArgs" /> instance containing the event data.</param>
        private void ApplicationButton_MouseEnter(object sender, MouseEventArgs e)
        {
            OfficePageApplicationButton officePageApplicationButton = sender as OfficePageApplicationButton;

            if (officePageApplicationButton == ImportButton
                && !(historicPanel.Content is ImportHistoric))
            {
                TimeoutAction.ClearTimeout();
                TimeoutAction.StartTimeout(delegate
                                               {
                                                   if (ImportHistoricPanel == null)
                                                   {
                                                       ImportHistoricPanel = new ImportHistoric();
                                                   }

                                                   historicPanel.Content = ImportHistoricPanel;
                                               });
            }
            else if (officePageApplicationButton == SearchButton
                     && !(historicPanel.Content is SearchHistoric))
            {
                TimeoutAction.ClearTimeout();
                TimeoutAction.StartTimeout(delegate
                                               {
                                                   if (SearchHistoricPanel == null)
                                                   {
                                                       SearchHistoricPanel = new SearchHistoric();
                                                   }

                                                   historicPanel.Content = SearchHistoricPanel;
                                               });
            }
            else if (officePageApplicationButton != ImportButton
                     && officePageApplicationButton != SearchButton)
            {
                TimeoutAction.ClearTimeout();
                TimeoutAction.StartTimeout(delegate { historicPanel.Content = null; });
            }
        }

        /// <summary>
        ///   Handles the MouseLeave event of the ApplicationButton control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.Windows.Input.MouseEventArgs" /> instance containing the event data.</param>
        private void ApplicationButton_MouseLeave(object sender, MouseEventArgs e)
        {
            TimeoutAction.ClearTimeout();
        }

        /// <summary>
        ///   Handles the Click event of the ChangePasswordHyperLinkButton control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void ChangePasswordHyperLinkButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ChangePasswordChildWindows window = new ChangePasswordChildWindows();

            window.Closed += delegate
                                 {
                                     if (window.DialogResult == true)
                                     {
                                         BusinessEntitiesDomainContext l_oBEDomainContext =
                                             new BusinessEntitiesDomainContext();
                                         l_oBEDomainContext.Disconnect();

                                         Redirection.RioOffice();
                                     }
                                 };

            window.ShowDialog();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitLabels();
        }
    }
}